package demo2.category;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

public class BookCategoryTree implements Serializable, CategoryTree<BookCategoryNode> {

    private BookCategoryNode self;
    private ArrayList<BookCategoryTree> children = new ArrayList<>();

    public BookCategoryTree() {
    }

    public BookCategoryTree(BookCategoryNode self) {
        this.self = self;
    }

    public void addCategorySubTree(BookCategoryTree subTree) {
        children.add(subTree);
    }

    @Override
    public int countChildren() {
        return children.size();
    }

    @Override
    public int recursivelyCountAllChildren() {
        int sum = children.size();
        for (BookCategoryTree subTree: children) {
            sum += subTree.recursivelyCountAllChildren();
        }
        return sum;
    }

    @Override
    public BookCategoryNode getSelf() {
        return self;
    }

    public void setSelf(BookCategoryNode self) {
        this.self = self;
    }

    @Override
    public Collection<BookCategoryTree> getChildren() {
        return children;
    }

    public void setChildren(Collection<BookCategoryTree> children) {
        this.children = new ArrayList<>(children);
    }
}
